package com.geek.novel.activity.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import com.geek.novel.event.EventHandler
import com.geek.novel.event.EventObservable
import com.geek.novel.event.EventObserver
import com.geek.novel.event.SimpleEventObservable


/**
 * 提供一些页面跳转事件
 */
open class EventActivity: AppCompatActivity(), EventHandler, EventObservable {

//    /**
//     * 界面跳转
//     */
//    open fun gotoActivity(ctx: Context, clazz: Class<out Any>){
//        this.gotoActivity(ctx, clazz, null)
//    }
//    /**
//     * 界面跳转
//     * @param params 页面跳转携带参数
//     */
//    open fun gotoActivity(ctx: Context, clazz: Class<out Any>, params: Bundle?){
//        this.gotoActivityForResult(ctx, clazz, params)
//    }

    /**解析意图*/
    open fun parseIntent(ctx: Context, clazz: Class<out Any>, params: Bundle?): Intent{
        val intent = Intent()
        intent.setClass(ctx, clazz)
        if(params != null){
            intent.putExtras(params)
        }
        return intent
    }
    /**
     * 界面跳转并获取结果
     */
    open fun gotoActivityForResult(intent: Intent, code: Int=0){
        if(code == 0){
            this.startActivity(intent)
        }else{
            this.startActivityForResult(intent, code)
        }
    }

    /**
     * 启动Activity转场动画
     */
    open fun startActivityForResultByAnim(ctx: Context, clazz: Class<out Any>, code: Int=0) {
        startActivityByAnim(ctx, clazz, null, code)
    }
    /**
     * 启动Activity转场动画
     */
    open fun startActivityForResultByAnim(ctx: Context, clazz: Class<out Any>, params: Bundle?=null, code: Int=0) {
        startActivityByAnim(ctx, clazz, params, code, android.R.anim.fade_in, android.R.anim.fade_out)
    }
    /**
     * 启动Activity转场动画
     */
    open fun startActivityByAnim(
        ctx: Context, clazz: Class<out Any>,
        params: Bundle?=null, code: Int=0,
        animIn: Int=android.R.anim.fade_in, animExit: Int=android.R.anim.fade_out) {
        val intent = this.parseIntent(ctx, clazz, params)
        gotoActivityForResult(intent, code)
        this.overridePendingTransition(animIn, animExit)
    }

    /**观察者*/
    private var eventObservable: SimpleEventObservable = SimpleEventObservable()

    /**
     * 添加观察者
     */
    override fun addEventObserver(observer: EventObserver) {
        this.eventObservable.addEventObserver(observer)
    }

    /**
     * 删除观察者
     */
    override fun removeEventObserver(observer: EventObserver) {
        this.eventObservable.removeEventObserver(observer)
    }

    /**
     * 通知单个观察者，后续需要通知全部再说
     */
    override fun notifyEvent(message: Message) {
        this.eventObservable.notifyEvent(message)
    }

}